package Text1;

import java.util.*;

public class Text {
    public static void main(String[] args) {
        vertex a = new vertex("A");
        vertex b = new vertex("B");
        vertex c = new vertex("C");
        vertex d = new vertex("D");
        vertex e = new vertex("E");
        vertex f = new vertex("F");
        vertex g = new vertex("G");

        a.edges.add(new edge(c));
        b.edges.add(new edge(c));
        c.edges.add(new edge(e));
        d.edges.add(new edge(e));
        e.edges.add(new edge(f));
        f.edges.add(new edge(g));

        List<vertex> picture = Arrays.asList(a,b,c,d,e,f,g);
        //拓扑排序
        //1.算入度
       for(vertex vertex:picture){
           for(edge  edge:vertex.edges){
               edge.linked.inpegree++;
           }
       }

//        for (vertex vertex:picture) {
//            System.out.println(vertex.name+" "+vertex.inpegree);
//        }
        //度数为0进队列
        Queue<vertex> queue = new LinkedList<>();
        for(vertex vertex:picture){
            if(vertex.inpegree==0){
                queue.offer(vertex);
            }
        }
       //排序
       while(!queue.isEmpty()){
           vertex v=queue.poll();
           System.out.println(v.name);
           for(edge  edge:v.edges){
               edge.linked.inpegree--;
               if(edge.linked.inpegree==0){
                   queue.offer(edge.linked);
               }
           }
       }
    }
}
